package com.l024.meet.ui;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.l024.framework.base.BasePageAdapter;
import com.l024.framework.base.BaseUIActivity;
import com.l024.framework.manager.MediaPlayerManager;
import com.l024.framework.utils.AnimUtils;
import com.l024.meet.MainActivity;
import com.l024.meet.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class GuideActivity extends BaseUIActivity implements View.OnClickListener {

    private ViewPager mViewPager;
    private ImageView ivMusicSwitch;
    private TextView tvGuideSkip;
    private ImageView ivGuidePoint1;
    private ImageView ivGuidePoint2;
    private ImageView ivGuidePoint3;
    private ImageView ivGuideStar;
    private ImageView ivGuideNight;
    private ImageView ivGuideSmile;
    /**
     * 1.ViewPager适配器
     * 2.小圆点逻辑
     * 3.歌曲播放
     * 4.属性动画旋转
     * 5.跳转
     */
    private View view1,view2,view3;
    private List<View> viewList = new ArrayList<>();
    private BasePageAdapter mBasePageAdapter;
    private MediaPlayerManager mediaPlayerManager;
    private ObjectAnimator mAnim;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        ivMusicSwitch = (ImageView) findViewById(R.id.iv_music_switch);
        tvGuideSkip = (TextView) findViewById(R.id.tv_guide_skip);
        ivGuidePoint1 = (ImageView) findViewById(R.id.iv_guide_point_1);
        ivGuidePoint2 = (ImageView) findViewById(R.id.iv_guide_point_2);
        ivGuidePoint3 = (ImageView) findViewById(R.id.iv_guide_point_3);
        //初始化View
        view1 = View.inflate(this,R.layout.layout_pager_guide_1,null);
        view2 = View.inflate(this,R.layout.layout_pager_guide_2,null);
        view3 = View.inflate(this,R.layout.layout_pager_guide_3,null);
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        mBasePageAdapter = new BasePageAdapter(viewList);
        //设置预加载
        mViewPager.setOffscreenPageLimit(viewList.size());
        mViewPager.setAdapter(mBasePageAdapter);
        //运行动画
        ivGuideStar = view1.findViewById(R.id.iv_guide_star);
        ivGuideNight = view2.findViewById(R.id.iv_guide_night);
        ivGuideSmile = view3.findViewById(R.id.iv_guide_smile);
        ((AnimationDrawable)ivGuideStar.getBackground()).start();
        ((AnimationDrawable)ivGuideNight.getBackground()).start();
        ((AnimationDrawable)ivGuideSmile.getBackground()).start();

        //小圆点逻辑
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectPotin(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //播放歌曲
        startMusic();
        //暂停歌曲
        ivMusicSwitch.setOnClickListener(this);
        tvGuideSkip.setOnClickListener(this);
    }

    /**
     * 动态选择小圆点
     */
    private void selectPotin(int position){
        switch (position){
            case 0:{
                ivGuidePoint1.setImageResource(R.drawable.img_guide_point_p);
                ivGuidePoint2.setImageResource(R.drawable.img_guide_point);
                ivGuidePoint3.setImageResource(R.drawable.img_guide_point);
                break;
            }
            case 1:{
                ivGuidePoint1.setImageResource(R.drawable.img_guide_point);
                ivGuidePoint2.setImageResource(R.drawable.img_guide_point_p);
                ivGuidePoint3.setImageResource(R.drawable.img_guide_point);
                break;
            }
            case 2:{
                ivGuidePoint1.setImageResource(R.drawable.img_guide_point);
                ivGuidePoint2.setImageResource(R.drawable.img_guide_point);
                ivGuidePoint3.setImageResource(R.drawable.img_guide_point_p);
                break;
            }
        }
    }

    /**
     * 歌曲播放
     */
    private void startMusic(){
        mediaPlayerManager = new MediaPlayerManager();
        Uri uri = Uri.parse("android.resource://com.l024.meet/" + R.raw.qhwt);
        mediaPlayerManager.setLooping(true);
        mediaPlayerManager.startPlay(GuideActivity.this,uri);

        //设置旋转
        //mAnim = ObjectAnimator.ofFloat(ivMusicSwitch,"rotation",0f,360f);
        ////2秒旋转一次
        //mAnim.setDuration(2*1000);
        ////是否重复
        //mAnim.setRepeatMode(ValueAnimator.RESTART);
        //mAnim.setRepeatCount(ValueAnimator.INFINITE);
        ////设置匀速差值器
        //mAnim.setInterpolator(new LinearInterpolator());
        mAnim = AnimUtils.rotation(ivMusicSwitch);
        mAnim.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mediaPlayerManager!=null){
            mediaPlayerManager.stopPlay();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_music_switch:{
                if(mediaPlayerManager!=null){
                    //正在播放
                    if(mediaPlayerManager.MEDIA_STATUS==MediaPlayerManager.MEDIA_STATUS_PLAY){
                        mediaPlayerManager.pausePlay();
                        ivMusicSwitch.setImageDrawable(getResources().getDrawable(R.drawable.img_guide_music_off));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT&&mAnim!=null) {
                            mAnim.pause();
                        }
                    }else if(mediaPlayerManager.MEDIA_STATUS==MediaPlayerManager.MEDIA_STATUS_PAUSE){
                        mediaPlayerManager.continuePlay();
                        if(mAnim!=null){
                            mAnim.start();
                        }
                        ivMusicSwitch.setImageDrawable(getResources().getDrawable(R.drawable.img_guide_music));
                    }
                }
                break;
            }
            case R.id.tv_guide_skip:{
                startActivity(new Intent(this,LoginActivity.class));
                finish();
                break;
            }
        }
    }
}
